<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/24
 * Time: 14:08
 */
namespace app\admin\controller\setting;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
class Config extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('FirmConfig');
    }

    public function index()
    {
        $row = $this->model->order('id','desc')->find();
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $lat_lng = map_bd2tx($params['lat'],$params['lng']);
            $params['lat'] = $lat_lng['lat'];
            $params['lng'] = $lat_lng['lng'];
            Db::startTrans();
            $result = false;
            try {
                if($row){
                    foreach($params as $key => $item){
                        $row->$key = $item;
                    }
                    $result = $row->save();
                }else{
                    $result = $this->model->allowField(true)->save($params);
                }
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }catch (PDOException $e){
                Db::rollback();
                $this->error($e->getMessage());
            }catch (ValidateException $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
            if($result !== false){
                $this->success("修改成功");
            }
            $this->error($this->model->getError());
        }
        $this->assign("row",$row);
        return $this->view->fetch();
    }

}